package com.jz.community.moduleshopping.orderDetail.task;

import android.app.Activity;

import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.net.ResponseCallback;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Response;

/**
 * Created by Tony on 2018/5/11.
 */
public class ApplyRefundsTask extends RxTask<String, Integer, BaseResponseInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;
    private Response mResponse;

    public ApplyRefundsTask(Activity activity,
                            ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected BaseResponseInfo doInBackground(String... params) {
        BaseResponseInfo info = null;

        // 执行请求
        String result = OkHttpUtil.patch(Constant.APPLY_REFUND_URL + params[0], new ResponseCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(Response response) {
                mResponse = response;
            }
        });
        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, BaseResponseInfo.class);
        } else {
            info = new BaseResponseInfo();
            info.setCode(mResponse.code());
        }
        return info;
    }

    @Override
    protected void onPostExecute(BaseResponseInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }
}
